package com.jeesite.modules.task.taskgroup.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.task.taskgroup.entity.DcSupTaskGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.task.taskgroup.entity.DcSupTaskGroupUnit;
import com.jeesite.modules.task.taskgroup.service.DcSupTaskGroupUnitService;

import java.util.ArrayList;
import java.util.List;

/**
 * dc_sup_task_group_unitController
 * @author xhc
 * @version 2022-06-28
 */
@Controller
@RequestMapping(value = "${adminPath}/task/dcSupTaskGroupUnit")
public class DcSupTaskGroupUnitController extends BaseController {

	@Autowired
	private DcSupTaskGroupUnitService dcSupTaskGroupUnitService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public DcSupTaskGroupUnit get(String id, boolean isNewRecord) {
		return dcSupTaskGroupUnitService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequestMapping(value = {"list", ""})
	public String list(DcSupTaskGroupUnit dcSupTaskGroupUnit, Model model) {
		model.addAttribute("dcSupTaskGroupUnit", dcSupTaskGroupUnit);
		return "modules/task/taskgroup/dcSupTaskGroupUnitList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<DcSupTaskGroupUnit> listData(DcSupTaskGroupUnit dcSupTaskGroupUnit, HttpServletRequest request, HttpServletResponse response) {
		Page<DcSupTaskGroupUnit> page = new Page<>(request, response);
		page.setPageSize(20000);
		dcSupTaskGroupUnit.setPage(page);
		 page = dcSupTaskGroupUnitService.findPage(dcSupTaskGroupUnit);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequestMapping(value = "form")
	public String form(DcSupTaskGroupUnit dcSupTaskGroupUnit, Model model) {
		model.addAttribute("dcSupTaskGroupUnit", dcSupTaskGroupUnit);
		return "modules/task/taskgroup/dcSupTaskGroupUnitForm";
	}

	/**
	 * 保存数据
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated DcSupTaskGroupUnit dcSupTaskGroupUnit) {
		dcSupTaskGroupUnitService.save(dcSupTaskGroupUnit);
		return renderResult(Global.TRUE, text("保存成功！"));
	}
	
	/**
	 * 停用数据
	 */
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(DcSupTaskGroupUnit dcSupTaskGroupUnit) {
		dcSupTaskGroupUnit.setStatus(DcSupTaskGroupUnit.STATUS_DISABLE);
		dcSupTaskGroupUnitService.updateStatus(dcSupTaskGroupUnit);
		return renderResult(Global.TRUE, text("停用机构成功"));
	}
	
	/**
	 * 启用数据
	 */
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(DcSupTaskGroupUnit dcSupTaskGroupUnit) {
		dcSupTaskGroupUnit.setStatus(DcSupTaskGroupUnit.STATUS_NORMAL);
		dcSupTaskGroupUnitService.updateStatus(dcSupTaskGroupUnit);
		return renderResult(Global.TRUE, text("启用机构成功"));
	}
	
	/**
	 * 删除数据
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(DcSupTaskGroupUnit dcSupTaskGroupUnit) {
		dcSupTaskGroupUnitService.delete(dcSupTaskGroupUnit);
		return renderResult(Global.TRUE, text("删除机构成功！"));
	}

	/**
	 * 返回机构
	 */
	@RequestMapping(value = "radio")
	@ResponseBody
	public DcSupTaskGroupUnit radio(DcSupTaskGroupUnit dcSupTaskGroupUnit) {
		List<String> orgs = new ArrayList<>();
		List<String> orgNames = new ArrayList<>();
		if(StringUtils.isNotBlank(dcSupTaskGroupUnit.getGroupId())){
		List<DcSupTaskGroupUnit> list = dcSupTaskGroupUnitService.findList(dcSupTaskGroupUnit);
		list.forEach(
				unit -> {
					orgs.add(unit.getUnitCode());
					orgNames.add(unit.getUnitName());
				}
		);
		dcSupTaskGroupUnit.setUnitCodes(StringUtils.join(orgs,","));
		dcSupTaskGroupUnit.setUnitNames(StringUtils.join(orgNames,","));
		}
		return dcSupTaskGroupUnit;
	}
}